/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package term_project;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Ross
 */
public class MainWindow extends JFrame 
{
    protected ComputerUser user;                                                // The logged in user
    
    protected JList friendList;                                                 // Friend list
    protected DefaultListModel friendModel;                                     // Friend Model
    
    protected JList chatList;                                                   // List of current chats
    protected DefaultListModel chatModel;                                       // Model for the list
    
    protected GridBagLayout layout;                                             // Layout
    protected GridBagConstraints c;                                             // Constraints
    
    protected JButton openChat;                                                 // Open chat button
    protected JButton removeChat;                                               // Removes chat
    protected JButton newChat;                                                  // New Chat button
    protected JPanel chatHolder;                                                // Chat panel
    
    protected JLabel friendLabel;                                               // Label for friend list
    protected JLabel chatLabel;                                                 // Label for chat list
    
    ChatWindow chatWindow;                                                      // This is where our chats will be displayed.
    
    public MainWindow(ComputerUser User)
    {
        super("Talks");
        user = User;
        initComponents();
        loadLists();
    }
    // *****************************
    // * Intializing functions     *
    // *****************************
    public void initComponents()
    // PRE:  To be run in the constructor
    // POST: Will set up the window
    {
        
        // *******************************
        // * Setting up the buttons      *
        // *******************************
        chatLabel = new JLabel("Active Chats");
        friendLabel = new JLabel("Friend Lists");
        friendLabel.setVerticalTextPosition(JLabel.BOTTOM);
        
        chatHolder = new JPanel();
        
        openChat = new JButton();
        openChat.setMaximumSize(new Dimension(40,40));
        openChat.setMinimumSize(new Dimension(40,40));
        openChat.setPreferredSize(new Dimension(40,40));
        openChat.setToolTipText("Open Chat");
        openChat.addActionListener(new ActionListener(){
                                                    public void actionPerformed(ActionEvent e)
                                                    {
                                                        openButtonPressed();
                                                    }
                                                    });
        removeChat = new JButton();
        removeChat.setMaximumSize(new Dimension(40,40));
        removeChat.setMinimumSize(new Dimension(40,40));
        removeChat.setPreferredSize(new Dimension(40,40));
        removeChat.setToolTipText("Remove Chat");
        removeChat.addActionListener(new ActionListener(){
                                                    public void actionPerformed(ActionEvent e)
                                                    {
                                                        removeButtonPressed();
                                                    }
                                                    });
        newChat = new JButton();
        newChat.setMaximumSize(new Dimension(40,40));
        newChat.setMinimumSize(new Dimension(40,40));
        newChat.setPreferredSize(new Dimension(40,40));
        newChat.setToolTipText("New Chat");
        newChat.addActionListener(new ActionListener(){
                                                    public void actionPerformed(ActionEvent e)
                                                    {
                                                        removeButtonPressed();
                                                    }
                                                    });
        
        try
       {
        Image img = ImageIO.read(this.getClass().getResource("open1.png"));
        openChat.setIcon(new ImageIcon(img));
        img = ImageIO.read(this.getClass().getResource("remove.png"));
        removeChat.setIcon(new ImageIcon(img));
        img = ImageIO.read(this.getClass().getResource("new.png"));
        newChat.setIcon(new ImageIcon(img));
       }
       catch(IOException e)
       {
           System.out.printf("Error: %s", e.toString());
       }
        chatHolder.add(openChat);
        chatHolder.add(removeChat);
        chatHolder.add(newChat);

        // *******************************
        // * Setting up the friends list *
        // *******************************
        friendModel = new DefaultListModel();                                   // Creates the friend model
        friendList = new JList(friendModel);                                    // Creates a list with the friend model
        friendList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);       // allows the user to only select one item
        friendList.setLayoutOrientation(JList.VERTICAL_WRAP);                   // Set the wrap to vertical
        friendList.setVisibleRowCount(-1);                                      // Still don't know
        
        // ********************************
        // * Setting up the chat list     *
        // ********************************
        chatModel = new DefaultListModel();                                     // Creates the friend model
        chatList = new JList(chatModel);                                        // Creates a list with the friend model
        chatList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);         // allows the user to only select one item
        chatList.setLayoutOrientation(JList.VERTICAL_WRAP);                     // Set the wrap to vertical
        chatList.setVisibleRowCount(-1);                                        // Still don't know
       
        // ********************************
        // * Formatting the layout        *
        // ********************************
        c = new GridBagConstraints();
        layout = new GridBagLayout();
        this.setLayout(layout);
        
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5,5,0,0);
        this.add(friendLabel, c);
        
        c.weightx = .5;
        c.insets = new Insets(5,0,0,0);
        c.gridx = 3;
        c.anchor = GridBagConstraints.LINE_START;
        this.add(chatLabel, c);
        
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(5,5,0,0);
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 3;
        c.gridheight = 3;
        c.weightx = .5;
        c.weighty = 1;
        this.add(friendList, c);
        
        c.insets = new Insets(5,5,0,5);
        c.gridx = 3;
        c.weightx = .5;
        c.gridwidth = 3;
        c.gridheight = 3;
        this.add(chatList, c);
        
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.CENTER;
        c.gridx = 3;
        c.gridy = 5;
        c.weightx = .5;
        c.weighty = 1;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.insets = new Insets(1,1,1,1);
        this.add(chatHolder, c);
        
        //c.anchor = GridBagConstraints.CENTER;
        //c.gridx = 4;
        //this.add(removeChat, c);
        
        //c.anchor = GridBagConstraints.LINE_START;
        //c.gridx = 5;
        //this.add(newChat, c);
        
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setMinimumSize(new Dimension(400,200));
        this.setVisible(true);
        
        chatWindow = new ChatWindow(user);
    }
    
    private void loadLists()
    {
        for(User users : user.getFriendsList())
        {
            friendModel.addElement(users.username);
        }
        for(Chat chats : user.getChatList())
        {
            chatModel.addElement(chats.getUsername());
        }
    }
    
    
    // ***********************
    // * Event Handlers      *
    // ***********************
    private void removeButtonPressed()
    {
        if(chatList.getSelectedIndex() == -1 )
        {
            System.out.println("Please select a chat");
        }
        else
        {
            chatWindow.removeChat(user.getChat(chatList.getSelectedIndex()));
            
        }
        
    }
    private void openButtonPressed()
    {
        if(chatList.getSelectedIndex() == -1 )
        {
            System.out.println("Please select a chat");
        }
        else
        {
            chatWindow.addChat(user.getChat(chatList.getSelectedIndex()));
            chatWindow.setVisible(true);
        }
    }

    
}
